* rifregtable.do	JEP		01/26/15
* Builds regression tables for third year paper version, updated from rifregtable.do
* This version does horizontal style tables, like Manacorda

*************************** Change log ********************************** 

* 01/10/18 		JEP		Change regression set and FE set


/*
	
*/	

cap program drop getfe
program define getfe, rclass
	* City Industry FE: All
	loc cxis="Yes"
	* City Employment: All
	loc cityemps="Yes"
	* Bartik price
	if strmatch("`1'","*bp*") loc bps="Yes"
	else loc bps=""
	* Bartik Quantity
	*if strmatch("`1'","*bq*") loc bqs="Yes"
	*else loc bqs=""
	* City Trends
	if strmatch("`1'","*sst*")  loc csts="Yes" 
	else if strmatch("`1'","*ct*")  loc csts="Yes" 
	else loc csts=""
	* Industry employment
	if strmatch("`1'","*ie*") loc ies="Yes"
	else loc ies=""
	return local cxi="`cxis'"
	return local tfe="Yes"
	return local cityemp="`cityemps'"
	return local bp="`bps'"
	return local cst="`csts'"
	return local ie="`ies'"	
	*return local bq="`bqs'"
	
	
end



cap program drop rifregtable2
program rifregtable2
	syntax, nrif(integer) time(string) panels(string) pctiles(numlist) master(string) outdir(string) mw(string) per(string) [dom(string) new(string) wrap makepdf caption(string)]  

	use "`master'/Data/Source/ocupados_mw.dta", clear
	* Find different panels from file names
	* For robustness checks, only the two last panels
	glo head ""
	glo botn ""
	* glo botr ""
	glo botcxi ""
	glo bottfe ""
	glo botcityemp ""
	glo botbp ""
	glo botcst ""
	glo botie ""
	glo botpib ""
	*glo botcfe ""
	* glo botbq ""
	
	
	
	* Get first and last pctile
	loc fw: word 1 of `pctiles'
	loc count: word count `pctiles'
	loc lw: word `count' of `pctiles'
	loc countnew: word count `new'
	
	foreach j in `pctiles' {
		glo cols ""
		glo betas ""
		glo ses ""
		glo ps ""
		glo colnum=1
		foreach pnl in `panels' {
			if `countnew' !=1 {
				loc neww : word ${colnum} of `new'
			}			
			glo cols "${cols}c"
			* Header - Column number
			if `j'==`fw' {
				glo head "${head} & (${colnum})"
			}
			* Centered columns
			est use "`master'/Ster/rif`nrif'`neww'/reg_`pnl'_tq(`time')_`mw'_salario_`per'_real_50`dom'_p`j'.ster"
			glo b:  di %10.4f _b[treat]
			glo se :  di %10.4f _se[treat]
			glo p=e(tp)
			if $p<0.001 glo star "***"
			else if $p<0.05 glo star "**"
			else if $p<0.1 glo star "*"
			else glo star ""
			glo p:  di %10.3f $p
			glo n= e(N)	
			* glo r = e(r2)
			* glo r: di %9.3f $r
			glo betas "${betas} & ${b}${star}"
			glo ses "${ses} & ($se )"
			glo ps "${ps} & [$p ]"	
						
			glo colnum = ${colnum}+1	
			
			if `j'==`lw' {
				* Bottom 1 - N
				glo botn "${botn} & $n"
				glo botr "${botr} & $r"
				getfe `pnl'
				glo botcxi "${botcxi} & `r(cxi)'"
				glo bottfe "${bottfe} & `r(tfe)'"
				glo botcityemp "${botcityemp} & `r(cityemp)'"
				glo botbp "${botbp} & `r(bp)'"
				glo botcst "${botcst} & `r(cst)'"
				glo botie "${botie} & `r(ie)'"
				if strmatch("`neww'","*pib_re*") {
					glo botpib "${botpib} & Yes"
				}
				else {
					glo botpib "${botpib} & "
				}
				*glo botcfe "${botcfe} & `r(cfe)'"
				
				*glo botbq "${botbq} & `r(bq)'"
				
				
			}
			
		}
		if `j'==`fw'  loc r "replace" 
		else loc r "append"	

		* cd "C:/Users/jorge_perez/Desktop/AA_Minimum_Wage/Doc/reg"
		texdoc init "`outdir'/`nrif'_`time'.tex", `r' force
		if `j'==`fw' {
			if "`wrap'"=="wrap" {
				tex \documentclass{article}
				tex \usepackage[letterpaper,left=3cm,right=3cm,top=3cm,bottom=3cm]{geometry}
				* tex \usepackage{adjustbox}
				tex \begin{document}
				tex \begin{table}
				tex \caption{`caption'}
				tex \centering
				tex \footnotesize
				* tex \begin{adjustbox}{center}
			}
			tex \begin{tabular}{l${cols}} \hline
		}
		if `j'==`fw' tex $head \\\hline
		tex p`j' $betas \\
		tex $ses \\
		tex $ps \\	
		if `j'==`lw' {
			tex \\\hline
			tex Observations $botn \\
			* tex R squared $botr \\
			tex City x Industry FE $botcxi \\
			* tex City FE $botcfe \\
			tex Time FE $bottfe \\
			tex Bartik Price Shocks $botbp \\
			* tex Bartik quantity shocks $botbq \\
			tex City Specific Trends  $botcst \\
			tex Industry Employment $botie \\
			tex Lagged Financial GDP $botpib \\
			
			
		}
		texdoc close		
	}
	texdoc init "`outdir'/`nrif'_`time'.tex", append force
	tex \hline
	tex \end{tabular}
	if "`wrap'"=="wrap"{ 
		* tex \end{adjustbox
		tex \end{table}
		tex \end{document}
	}
	texdoc close
	
	if "`makepdf'"=="makepdf" {
		cd "`outdir'"
		shell pdflatex "`outdir'/`nrif'_`time'.tex"
		! "`outdir'/`nrif'_`time'.pdf"
	}

end

	